class RequestAndComplaint::RegistrationRequestsController < ApplicationController
  # GET /request_and_complaint/registration_requests
  # GET /request_and_complaint/registration_requests.xml
  def index
    if (user_signed_in? && ([7,4,2].include?(current_user.role)))
       @title_view = 'Solicitud de Inscripcion'
       if (user_signed_in? && ([7,2].include?(current_user.role)))
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.state= 0 "])
       else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.state= 3 "])
       end

      @security_people = Security::Person.all
      @security_user_roles = Security::UserRole.all
      @users = User.find(:all,:conditions=>["users.role = ?", "4"])

      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @request_and_complaint_registration_requests }
        format.json  { render :json => @request_and_complaint_registration_requests }
      end
    else
       respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /request_and_complaint/registration_requests/1
  # GET /request_and_complaint/registration_requests/1.xml
  def show
    if (user_signed_in? && ([7,4,2].include?(current_user.role)))
      @title_view = 'Solicitud de Inscripcion'
      @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.find(params[:id])
      @departments = Shared::Department.all
      @provinces = []
      @districts = []
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @request_and_complaint_registration_request }
        format.json  { render :json => @request_and_complaint_registration_request }
      end
    else
       respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /request_and_complaint/registration_requests/new
  # GET /request_and_complaint/registration_requests/new.xml
  def new
  
      @title_view = 'Solicitud de Inscripcion'
      @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.new
      @departments = Shared::Department.all
      @provinces = []
      @districts = []
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @request_and_complaint_registration_request }
      end
   
    
  end

  # GET /request_and_complaint/registration_requests/1/edit
  def edit
    @title_view = 'Solicitud de Inscripcion'
    @departments = Shared::Department.all
    @provinces = []
    @districts = []
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.find(params[:id])
  end

  # POST /request_and_complaint/registration_requests
  # POST /request_and_complaint/registration_requests.xml
  def create
    @title_view = 'Solicitud de Inscripcion'
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.new(params[:request_and_complaint_registration_request])
    @departments = Shared::Department.all
#    @provinces = []
#    @districts = []
    if ! @request_and_complaint_registration_request.department.blank?
      @provinces = Shared::Province.find(:all,:conditions=>["shared_provinces.deleted = ? and (shared_provinces.department = ? )",0,@request_and_complaint_registration_request.department])
      if ! @request_and_complaint_registration_request.province.blank?
        @districts = Shared::District.find(:all,:conditions=>["shared_districts.deleted = ? and (shared_districts.province = ? )",0,@request_and_complaint_registration_request.province])
      else
        @districts = []
      end
    else
      @provinces = []
      @districts = []
    end

   

    respond_to do |format|
      if @request_and_complaint_registration_request.save
        format.html { redirect_to(@request_and_complaint_registration_request, :notice => 'Registration request was successfully created.') }
        format.xml  { render :xml => @request_and_complaint_registration_request, :status => :created, :location => @request_and_complaint_registration_request }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @request_and_complaint_registration_request.errors, :status => :unprocessable_entity }
      end
    end
     RegistrationRequestMailer.registered_email(@request_and_complaint_registration_request).deliver
  end

  # PUT /request_and_complaint/registration_requests/1
  # PUT /request_and_complaint/registration_requests/1.xml
  def update
    @title_view = 'Solicitud de Inscripcion'
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.find(params[:id])

    respond_to do |format|
      if @request_and_complaint_registration_request.update_attributes(params[:request_and_complaint_registration_request])
        format.html { redirect_to(@request_and_complaint_registration_request, :notice => 'Registration request was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @request_and_complaint_registration_request.errors, :status => :unprocessable_entity }
      end
    end
  end

  def change_provinces
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.new
    @provinces = Shared::Province.find(:all,:conditions=>{:deleted=>[0],:department=>params[:id]})
    @districts = []
    render :partial=>"provinces", :layout=>false
  end

  def change_districts
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.new
    @districts = Shared::District.find(:all,:conditions=>{:deleted=>[0],:province=>params[:id]})
    render :partial=>"districts", :layout=>false
  end


  def aprove

    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.find(params[:id])
    @request_and_complaint_registration_request.state=1
      if (user_signed_in?)
       @request_and_complaint_registration_request.user=current_user.id
    end
    @request_and_complaint_registration_request.save
     RegistrationRequestMailer.approved_email(@request_and_complaint_registration_request).deliver

   
    respond_to do |format|
      
      format.html { redirect_to(request_and_complaint_registration_requests_url) }
      format.xml  { head :ok }
    end
  end

  def reject
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.find(params[:id])
    @request_and_complaint_registration_request.state=2
     if (user_signed_in?)
       @request_and_complaint_registration_request.user=current_user.id
    end
    @request_and_complaint_registration_request.save

     RegistrationRequestMailer.rejected_email(@request_and_complaint_registration_request).deliver
    respond_to do |format|
      format.html { redirect_to(request_and_complaint_registration_requests_url) }
      format.xml  { head :ok }
    end
  end


  def assign
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.find(params[:id])
    @request_and_complaint_registration_request.state=3
    responsable = params[:registration_request_responsible]
    @request_and_complaint_registration_request.responsable=responsable
    @request_and_complaint_registration_request.save
    #RegistrationRequestMailer.rejected_email.deliver
    respond_to do |format|
      format.html { redirect_to(request_and_complaint_registration_requests_url) }
      format.xml  { head :ok }
    end
  end

  # DELETE /request_and_complaint/registration_requests/1
  # DELETE /request_and_complaint/registration_requests/1.xml
  def destroy
    @request_and_complaint_registration_request = RequestAndComplaint::RegistrationRequest.find(params[:id])
    @request_and_complaint_registration_request.deleted = 1
    @request_and_complaint_registration_request.save

    respond_to do |format|
      format.html { redirect_to(request_and_complaint_registration_requests_url) }
      format.xml  { head :ok }
    end
  end

  def update_table

     if (user_signed_in? && ([7].include?(current_user.role)))
    @search_id = params[:search_id]
    @search_name = params[:search_name]
@search_field = params[:field_name].to_s
    @search_state = params[:search_registration_request_state]


      case @search_field

	when "Nombre" then  @variable="request_and_complaint_registration_requests.name LIKE ? and "
	when "Codigo" then  @variable="request_and_complaint_registration_requests.id LIKE ? and "

    when "DNI" then  @variable="request_and_complaint_registration_requests.dni LIKE ? and "
 when "Todos" then  @variable="request_and_complaint_registration_requests.dni LIKE ? or request_and_complaint_registration_requests.name LIKE ? or request_and_complaint_registration_requests.id LIKE ? and "



	end

       if ! (@search_field=="Todos")
    if ! @search_id.blank?
      if ! @search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ? ",'%'+@search_id+'%',@search_name, @search_state])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.state = ?",'%'+@search_id+'%', @search_state])
      end
    else
      if !@search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ?",@search_name, @search_state])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.state = ?", @search_state])
      end
      
    end



      else
        if ! @search_id.blank?
      if ! @search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ? ",'%'+@search_id+'%','%'+@search_id+'%', '%'+@search_id+'%',@search_name, @search_state])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.state = ?",'%'+@search_id+'%','%'+@search_id+'%','%'+@search_id+'%', @search_state])
      end
    else
      if !@search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ?",@search_name, @search_state])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.state = ?", @search_state])
      end

    end

      end
    render :partial => "table"
     
    
     else
        if (user_signed_in? && ([4,2].include?(current_user.role)))
    @search_id = params[:search_id]
    @search_name = params[:search_name]
@search_field = params[:field_name].to_s
    @search_state = params[:search_registration_request_state]


      case @search_field

	when "Nombre" then  @variable="request_and_complaint_registration_requests.name LIKE ? and "
	when "Codigo" then  @variable="request_and_complaint_registration_requests.id LIKE ? and "

    when "DNI" then  @variable="request_and_complaint_registration_requests.dni LIKE ? and "

  when "Todos" then  @variable="request_and_complaint_registration_requests.dni LIKE ? or request_and_complaint_registration_requests.name LIKE ? or request_and_complaint_registration_requests.id LIKE ? and "


	end


        @user=current_user.id
     if ! (@search_field=="Todos")
    if ! @search_id.blank?
      if ! @search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ? and request_and_complaint_registration_requests.responsable = ?  ",'%'+@search_id+'%',@search_name, @search_state,@user])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.state = ? and request_and_complaint_registration_requests.responsable = ?",'%'+@search_id+'%', @search_state,@user])
      end
    else
      if !@search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ?  and request_and_complaint_registration_requests.responsable = ?",@search_name, @search_state,@user])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.state = ?  and request_and_complaint_registration_requests.responsable = ?", @search_state,@user])
      end
      
    end
        else

        if ! @search_id.blank?
      if ! @search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ? and request_and_complaint_registration_requests.responsable = ?  ",'%'+@search_id+'%','%'+@search_id+'%','%'+@search_id+'%', @search_name, @search_state,@user])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>[""+@variable+ "request_and_complaint_registration_requests.state = ? and request_and_complaint_registration_requests.responsable = ?",'%'+@search_id+'%','%'+@search_id+'%','%'+@search_id+'%', @search_state,@user])
      end
    else
      if !@search_name.blank?
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.name = ? and request_and_complaint_registration_requests.state = ?  and request_and_complaint_registration_requests.responsable = ?",@search_name, @search_state,@user])
      else
        @request_and_complaint_registration_requests = RequestAndComplaint::RegistrationRequest.find(:all,:conditions=>["request_and_complaint_registration_requests.state = ?  and request_and_complaint_registration_requests.responsable = ?", @search_state,@user])
      end

    end


        end

    render :partial => "table"
         
        
        
        
       end
    
    
    end
  end



end
